using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media.Imaging;

namespace Infralution.Localization.Wpf
{
	[ContentProperty("Children")]
	[MarkupExtensionReturnType(typeof(object))]
	public class ResxExtension1 : ManagedMarkupExtension1
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class Class228
		{
			public static readonly Class228 Instance9 = new Class228();

			public static Predicate<WeakReference> Instance9__15_0;

			internal bool method_0(WeakReference weakReference_0)
			{
				return weakReference_0.IsAlive;
			}
		}

		private string string_0;

		private string string_1;

		private string string_2;

		private string string_3;

		private ResourceManager resourceManager_0;

		private Binding binding_0;

		private Collection<ResxExtension1> collection_0 = new Collection<ResxExtension1>();

		private static Dictionary<string, WeakReference> dictionary_0 = new Dictionary<string, WeakReference>();

		private static MarkupExtensionManager1 markupExtensionManager1_0 = new MarkupExtensionManager1(40);

		[CompilerGenerated]
		private static GetResourceHandler1 getResourceHandler1_0;

		public static readonly DependencyProperty DefaultResxNameProperty = DependencyProperty.RegisterAttached("DefaultResxName", typeof(string), typeof(ResxExtension1), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits, smethod_0));

		public string ResxName
		{
			get
			{
				string text = string_0;
				if (string.IsNullOrEmpty(text))
				{
					if (string_1 == null)
					{
						WeakReference weakReference = base.TargetObjects.Find((WeakReference weakReference_0) => weakReference_0.IsAlive);
						if (weakReference != null && weakReference.Target is DependencyObject)
						{
							string_1 = (weakReference.Target as DependencyObject).GetValue(DefaultResxNameProperty) as string;
						}
					}
					text = string_1;
				}
				return text;
			}
			set
			{
				string_0 = value;
			}
		}

		public string Key
		{
			get
			{
				return string_2;
			}
			set
			{
				string_2 = value;
			}
		}

		public string DefaultValue
		{
			get
			{
				return string_3;
			}
			set
			{
				string_3 = value;
			}
		}

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public Collection<ResxExtension1> Children => collection_0;

		public Binding Binding
		{
			get
			{
				if (binding_0 == null)
				{
					binding_0 = new Binding();
				}
				return binding_0;
			}
		}

		[DefaultValue(null)]
		public string BindingElementName
		{
			get
			{
				return Binding.ElementName;
			}
			set
			{
				Binding.ElementName = value;
			}
		}

		[DefaultValue(null)]
		public PropertyPath BindingPath
		{
			get
			{
				return Binding.Path;
			}
			set
			{
				Binding.Path = value;
			}
		}

		[DefaultValue(null)]
		public RelativeSource BindingRelativeSource
		{
			get
			{
				return Binding.RelativeSource;
			}
			set
			{
				Binding.RelativeSource = value;
			}
		}

		[DefaultValue(null)]
		public object BindingSource
		{
			get
			{
				return Binding.Source;
			}
			set
			{
				Binding.Source = value;
			}
		}

		[DefaultValue(null)]
		public string BindingXPath
		{
			get
			{
				return Binding.XPath;
			}
			set
			{
				Binding.XPath = value;
			}
		}

		[DefaultValue(null)]
		public IValueConverter BindingConverter
		{
			get
			{
				return Binding.Converter;
			}
			set
			{
				Binding.Converter = value;
			}
		}

		[DefaultValue(null)]
		public string BindingGroupName
		{
			get
			{
				return Binding.BindingGroupName;
			}
			set
			{
				Binding.BindingGroupName = value;
			}
		}

		[DefaultValue(null)]
		public CultureInfo BindingConverterCulture
		{
			get
			{
				return Binding.ConverterCulture;
			}
			set
			{
				Binding.ConverterCulture = value;
			}
		}

		[DefaultValue(null)]
		public object BindingConverterParameter
		{
			get
			{
				return Binding.ConverterParameter;
			}
			set
			{
				Binding.ConverterParameter = value;
			}
		}

		[DefaultValue(false)]
		public bool BindsDirectlyToSource
		{
			get
			{
				return Binding.BindsDirectlyToSource;
			}
			set
			{
				Binding.BindsDirectlyToSource = value;
			}
		}

		[DefaultValue(BindingMode.Default)]
		public BindingMode BindingMode
		{
			get
			{
				return Binding.Mode;
			}
			set
			{
				Binding.Mode = value;
			}
		}

		[DefaultValue(false)]
		public bool BindingNotifyOnSourceUpdated
		{
			get
			{
				return Binding.NotifyOnSourceUpdated;
			}
			set
			{
				Binding.NotifyOnSourceUpdated = value;
			}
		}

		[DefaultValue(false)]
		public bool BindingNotifyOnTargetUpdated
		{
			get
			{
				return Binding.NotifyOnTargetUpdated;
			}
			set
			{
				Binding.NotifyOnTargetUpdated = value;
			}
		}

		[DefaultValue(false)]
		public bool BindingNotifyOnValidationError
		{
			get
			{
				return Binding.NotifyOnValidationError;
			}
			set
			{
				Binding.NotifyOnValidationError = value;
			}
		}

		[DefaultValue(null)]
		public object BindingAsyncState
		{
			get
			{
				return Binding.AsyncState;
			}
			set
			{
				Binding.AsyncState = value;
			}
		}

		[DefaultValue(false)]
		public bool BindingIsAsync
		{
			get
			{
				return Binding.IsAsync;
			}
			set
			{
				Binding.IsAsync = value;
			}
		}

		[DefaultValue(null)]
		public object BindingFallbackValue
		{
			get
			{
				return Binding.FallbackValue;
			}
			set
			{
				Binding.FallbackValue = value;
			}
		}

		[DefaultValue(null)]
		public object BindingTargetNullValue
		{
			get
			{
				return Binding.TargetNullValue;
			}
			set
			{
				Binding.TargetNullValue = value;
			}
		}

		[DefaultValue(false)]
		public bool BindingValidatesOnDataErrors
		{
			get
			{
				return Binding.ValidatesOnDataErrors;
			}
			set
			{
				Binding.ValidatesOnDataErrors = value;
			}
		}

		[DefaultValue(false)]
		public bool BindingValidatesOnExceptions
		{
			get
			{
				return Binding.ValidatesOnExceptions;
			}
			set
			{
				Binding.ValidatesOnExceptions = value;
			}
		}

		[DefaultValue(UpdateSourceTrigger.Default)]
		public UpdateSourceTrigger BindingUpdateSourceTrigger
		{
			get
			{
				return Binding.UpdateSourceTrigger;
			}
			set
			{
				Binding.UpdateSourceTrigger = value;
			}
		}

		[DefaultValue(false)]
		public Collection<ValidationRule> BindingValidationRules => Binding.ValidationRules;

		public static MarkupExtensionManager1 MarkupManager => markupExtensionManager1_0;

		private bool Boolean_0
		{
			get
			{
				if (binding_0 != null)
				{
					if (binding_0.Source == null && binding_0.RelativeSource == null && binding_0.ElementName == null && binding_0.XPath == null)
					{
						return binding_0.Path != null;
					}
					return true;
				}
				return false;
			}
		}

		private bool Boolean_1 => collection_0.Count > 0;

		private bool Boolean_2 => base.TargetPropertyType == typeof(Collection<ResxExtension1>);

		public static event GetResourceHandler1 GetResource
		{
			[CompilerGenerated]
			add
			{
				GetResourceHandler1 getResourceHandler = getResourceHandler1_0;
				GetResourceHandler1 getResourceHandler2;
				do
				{
					getResourceHandler2 = getResourceHandler;
					GetResourceHandler1 value2 = (GetResourceHandler1)Delegate.Combine(getResourceHandler2, value);
					getResourceHandler = Interlocked.CompareExchange(ref getResourceHandler1_0, value2, getResourceHandler2);
				}
				while ((object)getResourceHandler != getResourceHandler2);
			}
			[CompilerGenerated]
			remove
			{
				GetResourceHandler1 getResourceHandler = getResourceHandler1_0;
				GetResourceHandler1 getResourceHandler2;
				do
				{
					getResourceHandler2 = getResourceHandler;
					GetResourceHandler1 value2 = (GetResourceHandler1)Delegate.Remove(getResourceHandler2, value);
					getResourceHandler = Interlocked.CompareExchange(ref getResourceHandler1_0, value2, getResourceHandler2);
				}
				while ((object)getResourceHandler != getResourceHandler2);
			}
		}

		public ResxExtension1()
			: base(markupExtensionManager1_0)
		{
		}

		public ResxExtension1(string key)
			: base(markupExtensionManager1_0)
		{
			string_2 = key;
		}

		public override object ProvideValue(IServiceProvider serviceProvider)
		{
			object obj = null;
			RegisterTarget(serviceProvider);
			if (base.IsInDesignMode)
			{
				CultureManager1.smethod_1();
			}
			if (string.IsNullOrEmpty(Key) && !Boolean_0)
			{
				throw new ArgumentException("You must set the resource Key or Binding properties");
			}
			if (base.TargetProperty != null && !Boolean_2)
			{
				if (Boolean_1)
				{
					return method_1().ProvideValue(serviceProvider);
				}
				if (Boolean_0)
				{
					return method_0().ProvideValue(serviceProvider);
				}
				return GetValue();
			}
			return this;
		}

		public static void UpdateAllTargets()
		{
			markupExtensionManager1_0.UpdateAllTargets();
		}

		public static void UpdateTarget(string key)
		{
			foreach (ResxExtension1 activeExtension in markupExtensionManager1_0.ActiveExtensions)
			{
				if (activeExtension.Key == key)
				{
					activeExtension.UpdateTargets();
				}
			}
		}

		[AttachedPropertyBrowsableForChildren(IncludeDescendants = true)]
		public static string GetDefaultResxName(DependencyObject target)
		{
			return (string)target.GetValue(DefaultResxNameProperty);
		}

		public static void SetDefaultResxName(DependencyObject target, string value)
		{
			target.SetValue(DefaultResxNameProperty, value);
		}

		private Binding method_0()
		{
			Binding binding = new Binding();
			if (Boolean_0)
			{
				if (binding_0.ElementName != null)
				{
					binding.ElementName = binding_0.ElementName;
				}
				if (binding_0.RelativeSource != null)
				{
					binding.RelativeSource = binding_0.RelativeSource;
				}
				if (binding_0.Source != null)
				{
					binding.Source = binding_0.Source;
				}
				binding.AsyncState = binding_0.AsyncState;
				binding.BindingGroupName = binding_0.BindingGroupName;
				binding.BindsDirectlyToSource = binding_0.BindsDirectlyToSource;
				binding.Converter = binding_0.Converter;
				binding.ConverterCulture = binding_0.ConverterCulture;
				binding.ConverterParameter = binding_0.ConverterParameter;
				binding.FallbackValue = binding_0.FallbackValue;
				binding.IsAsync = binding_0.IsAsync;
				binding.Mode = binding_0.Mode;
				binding.NotifyOnSourceUpdated = binding_0.NotifyOnSourceUpdated;
				binding.NotifyOnTargetUpdated = binding_0.NotifyOnTargetUpdated;
				binding.NotifyOnValidationError = binding_0.NotifyOnValidationError;
				binding.Path = binding_0.Path;
				binding.TargetNullValue = binding_0.TargetNullValue;
				binding.UpdateSourceTrigger = binding_0.UpdateSourceTrigger;
				binding.ValidatesOnDataErrors = binding_0.ValidatesOnDataErrors;
				binding.ValidatesOnExceptions = binding_0.ValidatesOnExceptions;
				foreach (ValidationRule validationRule in binding_0.ValidationRules)
				{
					binding.ValidationRules.Add(validationRule);
				}
				binding.XPath = binding_0.XPath;
				binding.StringFormat = GetValue() as string;
			}
			else
			{
				binding.Source = GetValue();
			}
			return binding;
		}

		private MultiBinding method_1()
		{
			MultiBinding multiBinding = new MultiBinding();
			foreach (ResxExtension1 item in collection_0)
			{
				if (item.ResxName == null)
				{
					item.ResxName = ResxName;
				}
				multiBinding.Bindings.Add(item.method_0());
			}
			multiBinding.StringFormat = GetValue() as string;
			return multiBinding;
		}

		protected override object GetValue()
		{
			if (string.IsNullOrEmpty(Key))
			{
				return null;
			}
			object obj = null;
			if (!string.IsNullOrEmpty(ResxName))
			{
				try
				{
					if (getResourceHandler1_0 != null)
					{
						obj = getResourceHandler1_0(ResxName, Key, CultureManager1.UICulture);
					}
					if (obj == null)
					{
						if (resourceManager_0 == null)
						{
							resourceManager_0 = method_4(ResxName);
						}
						if (resourceManager_0 != null)
						{
							obj = resourceManager_0.GetObject(Key, CultureManager1.UICulture);
						}
					}
					if (!Boolean_2)
					{
						obj = method_5(obj);
					}
				}
				catch
				{
				}
			}
			if (obj == null)
			{
				obj = method_6(Key);
			}
			return obj;
		}

		protected override void UpdateTarget(object target)
		{
			if (Boolean_2)
			{
				return;
			}
			if (Boolean_1)
			{
				FrameworkElement frameworkElement = target as FrameworkElement;
				if (frameworkElement != null)
				{
					MultiBinding binding = method_1();
					frameworkElement.SetBinding(base.TargetProperty as DependencyProperty, binding);
				}
			}
			else if (Boolean_0)
			{
				FrameworkElement frameworkElement2 = target as FrameworkElement;
				if (frameworkElement2 != null)
				{
					Binding binding2 = method_0();
					frameworkElement2.SetBinding(base.TargetProperty as DependencyProperty, binding2);
				}
			}
			else
			{
				base.UpdateTarget(target);
			}
		}

		private bool method_2(Assembly assembly_0, string string_4)
		{
			if (assembly_0.GetType().Name == "AssemblyBuilder")
			{
				return false;
			}
			try
			{
				string[] manifestResourceNames = assembly_0.GetManifestResourceNames();
				string text = string_4.ToLower() + ".resources";
				string[] array = manifestResourceNames;
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i].ToLower() == text)
					{
						return true;
					}
				}
			}
			catch
			{
			}
			return false;
		}

		private Assembly method_3()
		{
			Assembly entryAssembly = Assembly.GetEntryAssembly();
			if (entryAssembly != null && method_2(entryAssembly, ResxName))
			{
				return entryAssembly;
			}
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			int num = 0;
			Assembly assembly;
			while (true)
			{
				if (num < assemblies.Length)
				{
					assembly = assemblies[num];
					string fullName = assembly.FullName;
					if (!fullName.StartsWith("Microsoft.") && !fullName.StartsWith("System.") && !fullName.StartsWith("System,") && !fullName.StartsWith("mscorlib,") && !fullName.StartsWith("PresentationFramework,") && !fullName.StartsWith("WindowsBase,") && method_2(assembly, ResxName))
					{
						break;
					}
					num++;
					continue;
				}
				return null;
			}
			return assembly;
		}

		private ResourceManager method_4(string string_4)
		{
			WeakReference value = null;
			ResourceManager resourceManager = null;
			if (string_4 == null)
			{
				return null;
			}
			if (dictionary_0.TryGetValue(string_4, out value))
			{
				resourceManager = value.Target as ResourceManager;
				if (resourceManager == null)
				{
					dictionary_0.Remove(string_4);
				}
			}
			if (resourceManager == null)
			{
				Assembly assembly = method_3();
				if (assembly != null)
				{
					resourceManager = new ResourceManager(string_4, assembly);
				}
				dictionary_0.Add(string_4, new WeakReference(resourceManager));
			}
			return resourceManager;
		}

		[DllImport("gdi32.dll")]
		private static extern bool DeleteObject(IntPtr intptr_0);

		private object method_5(object object_1)
		{
			object obj = null;
			BitmapSource bitmapSource = null;
			if (object_1 is Icon)
			{
				Icon icon = object_1 as Icon;
				using MemoryStream memoryStream = new MemoryStream();
				icon.Save(memoryStream);
				memoryStream.Seek(0L, SeekOrigin.Begin);
				bitmapSource = BitmapFrame.Create(memoryStream);
			}
			else if (object_1 is Bitmap)
			{
				IntPtr hbitmap = (object_1 as Bitmap).GetHbitmap();
				bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
				bitmapSource.Freeze();
				DeleteObject(hbitmap);
			}
			if (bitmapSource != null)
			{
				obj = ((!(base.TargetPropertyType == typeof(object))) ? ((object)bitmapSource) : ((object)new System.Windows.Controls.Image
				{
					Source = bitmapSource,
					Width = bitmapSource.Width,
					Height = bitmapSource.Height
				}));
			}
			else
			{
				obj = object_1;
				Type targetPropertyType = base.TargetPropertyType;
				if (targetPropertyType != null && object_1 is string && targetPropertyType != typeof(string) && targetPropertyType != typeof(object))
				{
					obj = TypeDescriptor.GetConverter(targetPropertyType).ConvertFromInvariantString(object_1 as string);
				}
			}
			return obj;
		}

		private object method_6(string string_4)
		{
			object result = string_3;
			Type targetPropertyType = base.TargetPropertyType;
			if (string_3 == null)
			{
				if (targetPropertyType == typeof(string) || targetPropertyType == typeof(object) || Boolean_2)
				{
					result = "#" + string_4;
				}
			}
			else if (targetPropertyType != null && targetPropertyType != typeof(string) && targetPropertyType != typeof(object))
			{
				try
				{
					result = TypeDescriptor.GetConverter(targetPropertyType).ConvertFromInvariantString(string_3);
					return result;
				}
				catch
				{
					return result;
				}
			}
			return result;
		}

		private static void smethod_0(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (!DesignerProperties.GetIsInDesignMode(dependencyObject_0))
			{
				return;
			}
			foreach (ResxExtension1 activeExtension in MarkupManager.ActiveExtensions)
			{
				if (activeExtension.IsTarget(dependencyObject_0))
				{
					activeExtension.resourceManager_0 = null;
					activeExtension.string_1 = dependencyPropertyChangedEventArgs_0.NewValue as string;
					activeExtension.UpdateTarget(dependencyObject_0);
				}
			}
		}
	}
}
